/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.comut.crud.util;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 *
 * @author asantos
 */
public class Util {

    public static String cutFirstAndLastChars(String str) {
        if (str.startsWith("'")) {
            str = str.substring(1);
        }
        if (str.endsWith("'")) {
            str = str.substring(0, str.lastIndexOf("'"));
        }

        return str;
    }

    /**
     * Issue: IFL-1566
     */
    public static String generateUUID() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }

    /**
     * Issue: IFL-1569 Gets a date with the format yyyy-mm-ddThh:mm:ss
     *
     * @param date
     * @return
     */
    public static String getDate(Date date) {
        String ret = "";

        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            ret = String.format("%04d-%02d-%02dT%02d:%02d:%02d", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
        }

        return ret;
    }

    /**
     * Issue: IFL-1566
     *
     * @param lat
     * @param lon
     * @return
     */
    public static String getFractal(Double lat, Double lon) {
        StringBuilder buffer = new StringBuilder();
        String formattedAddress = "";

        try {
            String geoCodingURL = SystemProperties.getGeoCodingURL();
            geoCodingURL = geoCodingURL.replaceAll("@lat", lat.toString());
            geoCodingURL = geoCodingURL.replaceAll("@lon", lon.toString());

            HttpClient hc = new DefaultHttpClient();
            HttpGet get = new HttpGet(geoCodingURL);
            HttpResponse response = hc.execute(get);
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String data = "";

            while (data != null) {
                data = rd.readLine();
                if (data != null) {
                    buffer.append(data);
                }
            }

            rd.close();
            JsonParser jsobParser = new JsonParser();
            JsonObject jsonObject = (JsonObject) jsobParser.parse(buffer.toString());
            formattedAddress = jsonObject.get("formatted_address").getAsString();
        } catch (Exception ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        }

        return formattedAddress;
    }
}
